package Singleton;

import java.io.Serializable;

// 饿汉式 ： 不用也会创建
public class Singleton1 implements Serializable {
    private Singleton1(){
        if (INSTANCE!=null){  // if语句放止反射破坏单例模式
            throw new RuntimeException("单例对象不能重复创建");
        }
        System.out.println("所有单例模式中构造器都需要私有化");
    }

    private static final Singleton1 INSTANCE = new Singleton1();

    public static Singleton1 getInstance() {
        return INSTANCE;
    }

    public static void otherMethod(){
        System.out.println("Singleton1：otherMethod");
    }

    // 放止反序列化破坏单例模式
    public Object readResolve(){
        return INSTANCE;
    }
}
